<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i> 管理员管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          style="float: right;margin-bottom:20px; "
          type="primary"
          icon="el-icon-lx-add"
          class="handle-del mr10"
          @click="addUser"
        >添加
        </el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="username" label="管理员名称" align="center"></el-table-column>
        <el-table-column label="管理员组" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.group_id==1" >超级管理员</span>
            <span v-if="scope.row.group_id==2" >访客</span>
            <span v-if="scope.row.group_id==3" >普通管理员</span>
          </template>
        </el-table-column>

        <el-table-column prop="ip" label="最后登录IP" align="center"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
        <el-table-column prop="last_time" label="最后登录时间" align="center"></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >修改
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="addform" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="addform.username"></el-input>
        </el-form-item>
        <el-form-item label="用户组">
          <el-select v-model="addform.group_id" placeholder="选择用户组" class="handle-select mr10">
            <el-option key="超级管理员" label="超级管理员" value="1"></el-option>
            <el-option key="访客" label="访客" value="2"></el-option>
            <el-option key="普通管理员" label="普通管理员" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addform.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="用户组">
          <el-select v-model="``+form.group_id" placeholder="选择用户组" class="handle-select mr10">
            <el-option key="1" label="超级管理员" value="1"></el-option>
            <el-option key="2" label="访客" value="2"></el-option>
            <el-option key="3" label="普通管理员" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input disabled="true" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        name: 'basetable',
        data () {
            return {
                query: {
                    name: '',
                    page: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                total: 0,
                form: {},
                idx: -1,
                id: -1,
                addVisible: false,
                addform: {}
            }
        },
        created () {
            this.getData()
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData () {
                this.$api.admin.list({}).then(res => {
                    this.tableData = res.data
                    this.total = res.total || 0
                })
            },
            // 触发搜索按钮
            handleSearch () {
                this.$set(this.query, 'page', 1)
                this.getData()
            },
            // 删除操作
            handleDelete (index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {
                            this.$api.admin.delete({id: row.id}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,' + res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
            },
            // 多选操作
            handleSelectionChange (val) {
                this.multipleSelection = val
            },
            delAllSelection () {
                const length = this.multipleSelection.length
                var arr = []
                this.delList = this.delList.concat(this.multipleSelection)
                for (let i = 0; i < length; i++) {
                    arr.push(this.multipleSelection[i].id)
                }
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {
                            this.$api.user.delete({ids: arr}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,' + res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
                this.multipleSelection = []
            },
            //添加用户
            addUser () {
                this.addVisible = true
            },
            addUserSubmit () {
                this.$api.admin.add(this.addform).then(res => {
                    if (res.code == 1) {
                        this.$message.error(res.msg)
                        return
                    }
                    this.addVisible = false
                    this.$message.success(`添加成功`)
                    this.getData()
                })
            },
            // 编辑操作
            handleEdit (index, row) {
                this.idx = index
                this.form = row
                this.editVisible = true
            },
            // 保存编辑
            saveEdit () {
                this.$api.admin.update(this.form).then(res => {
                    if (res.code == 1) {
                        this.$message.error(`修改失败,` + res.msg)
                        return
                    }
                    this.editVisible = false
                    this.$message.success(`修改成功`)
                    this.$set(this.tableData, this.idx, this.form)
                })
            },
            // 分页导航
            handlePageChange (val) {
                this.$set(this.query, 'page', val)
                this.getData()
            }
        }
    }
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }
</style>
